-
Notifications
You must be signed in to change notification settings - Fork 426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "lexical context" information to the macro expansion context #1554
Conversation
@swift-ci please test |
db6a793
to
63606b0
Compare
@swift-ci please test |
if firstName.text != "_", parameterHasArgumentLabelByDefault | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if firstName.text != "_", parameterHasArgumentLabelByDefault | |
{ | |
if firstName.text != "_", parameterHasArgumentLabelByDefault { |
extension SyntaxProtocol { | ||
/// Form a function name. | ||
private func formFunctionName( | ||
_ baseName: String, _ parameters: ParameterClauseSyntax?, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
baseName
could just be a TokenSyntax
. Then you could use .init
instead of "init"
below
|
||
// Extensions | ||
if let extensionDecl = self.as(ExtensionDeclSyntax.self) { | ||
// FIXME: It would be nice to be able to switch on type syntax... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could switch over extensionDecl.kind
and then get the meta type using SyntaxKind.syntaxNodeType
. You could also use that above.
return #""<unknown>""# | ||
} | ||
|
||
return ExprSyntax("\(literal: name)").with(\.leadingTrivia, node.leadingTrivia) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably also want to retain the node’s trailing trivia, right?
@@ -769,6 +889,70 @@ final class MacroSystemTests: XCTestCase { | |||
) | |||
} | |||
|
|||
func testPoundFunction() { | |||
assertMacroExpansion( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to have a separate assertMacroExpansion
invocation for each #function
. In my experience that makes it a lot easier to
a) check how #function
is being transformed in each case and
b) debug the test case if something is going wrong
The lexical context of a particular macro expansion involves all of the enclosing contexts, including functions, types and extensions, properties and subscripts, and so on. The lexical context stack can be used to, for example, determine which type a macro was expanded within, or gather the parameters of the enclosing function for an assertion, etc.
63606b0
to
5c47c90
Compare
@swift-ci please test |
/// blocks removed. | ||
/// | ||
/// The first entry in the array is the innermost context, which could be | ||
/// the syntax node to which |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also an unfinished comment.
Sources/SwiftCompilerPluginMessageHandling/PluginMessages.swift
Outdated
Show resolved
Hide resolved
@swift-ci please test |
@swift-ci please test Windows |
1 similar comment
@swift-ci please test Windows |
@swift-ci test Windows |
This PR conditionally adopts the new `lexicalContext` member of `MacroExpansionContext` added in swiftlang/swift-syntax#1554. If the SwiftSyntax600 pseudo-module is available, then this member should be available for use and can be used to perform additional diagnostics for tests and to get the names of their containing types. With this PR, if built against an older toolchain (5.11 or earlier), the old hacky "is there leading whitespace?" mechanism is still used. A future PR will recursively perform suite-level diagnostics on the lexical contexts containing tests and suites, so that a test cannot be (easily) inserted into a type that cannot be used as a suite. Resolves rdar://109439578.
This PR conditionally adopts the new `lexicalContext` member of `MacroExpansionContext` added in swiftlang/swift-syntax#1554. If the SwiftSyntax600 pseudo-module is available, then this member should be available for use and can be used to perform additional diagnostics for tests and to get the names of their containing types. With this PR, if built against an older toolchain (5.11 or earlier), the old hacky "is there leading whitespace?" mechanism is still used. A future PR will recursively perform suite-level diagnostics on the lexical contexts containing tests and suites, so that a test cannot be (easily) inserted into a type that cannot be used as a suite. Resolves rdar://109439578. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
The lexical context of a particular macro expansion involves all of the
enclosing contexts, including functions, types and extensions,
properties and subscripts, and so on. The lexical context stack can be
used to, for example, determine which type a macro was expanded
within, or gather the parameters of the enclosing function for an
assertion, etc.